home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / SDKs / Word Services SDK 1.0.6 / Writeswell Jr 1.2.1 Sources ƒ / Writeswell Jr. Source / GenHandlers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-16  |  4.1 KB  |  139 lines  |  [TEXT/KAHL]

  1. /* GenHandlers.c
  2.  * Do the generic event handling according to Richard Clarke from Dev U.
  3.  * ©1992 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  * 18 Dec 91 Mike Crawford
  14.  */
  15.  
  16. #include <AppleEvents.h>
  17. #include <AEObjects.h>
  18. #include <AERegistry.h>
  19. #include "TBConstants.h"
  20. #include "TBGlobals.h"
  21. #include "GenHandlers.h"
  22. #include "ObNull.h"
  23. #include "ObWind.h"
  24. #include "ObText.h"
  25. #include "ObOspec.h"
  26. #include "Gripe.h"
  27.  
  28. static AEEventHandlerUPP gGenericHandlerUPP = (AEEventHandlerUPP)NULL;
  29.  
  30. OSErr InitGenericHandlers( void )
  31. {
  32.     OSErr err;
  33.  
  34.     gGenericHandlerUPP = NewAEEventHandlerProc( GenericHandler );
  35.     if ( !gGenericHandlerUPP )
  36.         return memFullErr;
  37.  
  38.     if ( err = AEInstallEventHandler( kAECoreSuite,
  39.                                 typeWildCard,
  40.                                 (AEEventHandlerUPP)gGenericHandlerUPP,
  41.                                 0,
  42.                                 false ) ){
  43.         Gripe( "\poapp install failed" );
  44.         return err;
  45.     }
  46.     return err;
  47. }
  48.  
  49. OSErr TearDownGenerichandlers( void )
  50. {
  51. #ifdef GENERATINGCFM
  52.     // We don't want to dispose of the UPP if we are not running
  53.     // under the code fragment manager, as the UPP is an actual
  54.     // pointer to a function in that case.
  55.     
  56.     if ( gGenericHandlerUPP )
  57.         DisposeRoutineDescriptor( gGenericHandlerUPP );
  58.  
  59. #endif
  60.     return noErr;
  61. }
  62.  
  63. /* In the generic handler, we extract the direct object, resolve it, and identify
  64.  * the type of the finally resolved object (window, text, etc.).  Then we call a routine
  65.  * that handles events for that kind of object.
  66.  *
  67.  * Note that this essentially circumvents the AppleEvent handler dispatch table, and
  68.  * replaces it with our own.  The reason is that it makes it possible to have a separate
  69.  * source file for each kind of data object that will not need to be changed if other
  70.  * sorts of objects are added.  If we have separate event handlers, then when we add
  71.  * an object, we will need to add the code to handle that object to each event handler.
  72.  * In this scheme, if we add support for a new kind of object, we add a source file
  73.  * full of handlers for that object, and add an entry (and a new #include line) in this
  74.  * file.
  75.  *
  76.  * This should make the code more maintainable.  It would be real natural to use an
  77.  * object-oriented language here, where each object would have a standard set of methods
  78.  * for each event that all objects are supposed to handle.
  79.  */
  80.  
  81. pascal OSErr GenericHandler( AppleEvent *theAppleEventPtr,
  82.                                 AppleEvent *replyEventPtr,
  83.                                 long refCon )
  84. {
  85.     AEDesc paramDesc;
  86.     AEDesc tokenDesc;
  87.     OSErr err;
  88.  
  89.     err = AEGetParamDesc( theAppleEventPtr,
  90.                             keyDirectObject,
  91.                             typeObjectSpecifier,
  92.                             ¶mDesc );
  93.  
  94.     if ( err && err != errAEDescNotFound ){        /* It's OK for no parameter to be there */
  95.         /*Gripe( "\pAEGetParamDesc failed" );*/
  96.         return err;
  97.     }
  98.     
  99.     if ( err != errAEDescNotFound ){
  100.         err = AEResolve( ¶mDesc,
  101.                             kAEIDoMinimum,
  102.                             &tokenDesc );
  103.         if ( err ){
  104.             return err;
  105.         }
  106.     }else{
  107.         /* Copy the null param to the token desc */
  108.         err = AEDuplicateDesc( ¶mDesc, &tokenDesc );
  109.         if ( err ){
  110.             Gripe( "\pAEDuplicateDesc failed" );
  111.             return err;
  112.         }
  113.     }
  114.     
  115.     switch ( tokenDesc.descriptorType ){
  116.     
  117.         case typeNull:
  118.             err = DispatchNull( &tokenDesc, theAppleEventPtr, replyEventPtr, refCon );
  119.             break;
  120.         case cWindow:
  121.             err = DispatchWind( &tokenDesc, theAppleEventPtr, replyEventPtr, refCon );
  122.             break;
  123.         case typeTEText:
  124.             err = DispatchTEText( &tokenDesc, theAppleEventPtr, replyEventPtr, refCon );
  125.             break;
  126.         case typeOSpecToken:
  127.             err = DispatchOspec( &tokenDesc, theAppleEventPtr, replyEventPtr, refCon );
  128.             break;
  129.         default:
  130.             Gripe( "\pUnknown object type" );
  131.             return errAEEventNotHandled;
  132.         
  133.     }
  134.     
  135.     err = AEDisposeDesc( &tokenDesc );        /* 1.1.1 MDC fix a memory leak */
  136.                     
  137.     return err;
  138. }
  139.